<?php
class Session {
    
    private $_roles;
    
    public static function init(){
        session_start();
    }
    public static function destroy($clave = false){
        if($clave){
            if(is_array($clave)){
                for($i = 0 ; $i < count($clave); $i++){
                    if(isset($_SESSION[$clave[$i]])){
                        unset($_SESSION[$clave[$i]]);
                    }
                }
            }else{
                unset($_SESSION[$clave]);
            }
        }else{
            session_destroy();
        }
    }
    
    public static function set($clave, $valor){
        $_SESSION[$clave] = $valor;
    }
    
    public static function get($clave){
        if(isset($_SESSION[$clave]))
            return $_SESSION[$clave];
    }
    
    public static function access($level){
        if(!Session::get('access')){
            header('location:' . BASE_URL . "error/access/405");
            exit;
        }
        
        Session::tiempo();
        
        if(Session::getLevel($level) > Session::getLevel(Session::get('level'))){
            header('location:' . BASE_URL . "error/access/405");
            exit;
        }
    }
    
    public static function accessView($level){
        if(!Session::get('access')){
            return false;
        }
        
        if(Session::getLevel($level) > Session::getLevel(Session::get('level'))){
            return false;
        }
        return true;
    }
    
    public static function getLevel($level){
        
        $model = new Model();
        $roles = $model->lista("
            SELECT *
            FROM adic.cat_auditores
        ");
        
        foreach($roles as $rol){
            $role[$rol['descrip']] = $rol['id'];
        }
        
        //print_r($role);exit;
        if(!array_key_exists($level,$role)){
            throw new Exception("405");
        }else{
            return $role[$level];
        }
    }
    
    public static function accessStrict(array $level, $noAdmin = false){
        if(!Session::get('access')){
            header('location:' . BASE_URL . "error/access/5050");
            exit;
        }
        
        Session::tiempo();
        
        if(!$noAdmin == false){
            if(Session::get('level') == 'admin'){
                return ;
            }
        }
        
        if(count($level)){
            if(in_array(Session::get('level'), $level)){
                return;
            }
        }
        header('location:' . BASE_URL . 'error/access/5050');
    }
    
    public static function accessViewStrict(array $level, $noAdmin = false){
        if(!Session::get('access')){
            return false;
        }
        
        if(!$noAdmin == false){
            if(Session::get('level') == 'admin'){
                return true;
            }
        }
        
        if(count($level)){
            if(in_array(Session::get('level'), $level)){
                return true;
            }
        }
        return false;
    }
    
    public static function tiempo(){
        if(!Session::get('time')  || !defined('SESSION_TIME')){
            throw new Exception('101');
        }
        
        if(SESSION_TIME == 0){
            return;
        }
        
        if(time() - Session::get('time') > (SESSION_TIME * 60)){
            Session::destroy();
            header('location:' . BASE_URL . "error/access/8080");
        }else{
            Session::set('time',time());
        }
    }
}
?>